//
//  ChangePayPwdViewController.m
//  EasyToTesco
//
//  Created by mc on 16/3/2.
//  Copyright © 2016年 Jack. All rights reserved.
//

#import "ChangePayPwdViewController.h"
#import "ReadLoginData.h"
#import "AFNetworking.h"
#define WIDTH [[UIScreen mainScreen] bounds].size.width
#define NORMWIDTH [[UIScreen mainScreen] bounds].size.width / 375
#define HEIGHT [[UIScreen mainScreen] bounds].size.height
#define NORMHEIGHT [[UIScreen mainScreen] bounds].size.height / 667
#define FRAME [[UIScreen mainScreen] bounds]
@interface ChangePayPwdViewController ()<UIAlertViewDelegate>

{
    NSInteger orderstate;
    BOOL BtnState;
    NSString *usernameStr;
}


@property (nonatomic, copy)NSString *str;


@end

@implementation ChangePayPwdViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSDictionary *dic = [ReadLoginData readLoginData];
    if (dic.allKeys.count == 0) {
        UIAlertView *alview = [[UIAlertView alloc] initWithTitle:@"提示" message:@"您尚未登录,请登录后再次操作" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        alview.tag = 123303;
        [alview show];
    }else {
        usernameStr = [dic[@"data"] objectForKey:@"username"];
        
        [self addMyOrderTopView];
        [self data];
        [self userLockPhone:usernameStr];
        
    }
    
    // Do any additional setup after loading the view from its nib.
    
    

    
}




- (void)data{
    self.userName.text = usernameStr;
    
}















- (IBAction)changePwdButton:(id)sender {
    
    AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
    mgr.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    NSDictionary *dic= @{@"username":usernameStr,
                         @"userpwd	":self.changePassWord.text,
                         @"verify":self.verify.text,
                         @"password": self.passWord.text
                         };
    [mgr GET:@"http://www.ylgw8.com/AppUser/Setpwd" parameters:dic success:^(NSURLSessionDataTask *task, id responseObject) {
        NSDictionary *dict = [self toArrayOrNSDictionary:responseObject];
        NSLog(@"%@, %@", dict, dict[@"message"]);

        
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"失败");
    }];
    
    
}



- (void)userLockPhone: (NSString *)str{
    // http://www.ylgw8.com/AppUser/Getphone
    
    
    
    
    AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
    mgr.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    NSDictionary *dic= @{@"username":usernameStr,
                                                 };
    [mgr GET:@"http://www.ylgw8.com/AppUser/Getphone" parameters:dic success:^(NSURLSessionDataTask *task, id responseObject) {
        NSDictionary *dict = [self toArrayOrNSDictionary:responseObject];
        NSLog(@"~~~~~~%@, ~~~~%@", dict, dict[@"message"]);
        if ([dict[@"message"] isEqualToString:@"用户不存在或没有绑定手机"]) {
            NSLog(@"1");
            
            UIAlertView *alview = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请先绑定手机" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            
            [alview show];
            
            
            
        }else{
            self.phone.text = dict[@"phone"];
        }
        
        
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"失败");
    }];
    

    
    
    
    
    
}




- (IBAction)sendVerify:(id)sender {
    [self addPhoneMessage];
    
}







- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
}



//获取短信验证码数据
- (void)addPhoneMessage {
    AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
    mgr.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    NSString *username = usernameStr;
    NSString *type = @"1";
    NSString *phone = self.phone.text;
    
    
    NSLog(@"验证码%@====%@", username, phone);
    
    NSDictionary *param = @{@"username":[NSString stringWithFormat:@"%@", username], @"type":[NSString stringWithFormat:@"%@", type], @"phone":[NSString stringWithFormat:@"%@", phone]};
    
    [mgr POST:@"http://www.ylgw8.com/AppPub/Getcode" parameters:param success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSDictionary *dict = [self toArrayOrNSDictionary:responseObject];
        NSLog(@"短信发送%@", dict[@"message"]);
        
        //        [self addphonenumdatasources];
        UIAlertView *alview = [[UIAlertView alloc] initWithTitle:@"" message:[NSString stringWithFormat:@"%@", dict[@"message"]] delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alview show];
    } failure:^(AFHTTPRequestOperation *operation,NSError *error)
     {
         
         //
     }];
}

// 将JSON串转化为字典或者数组
- (id)toArrayOrNSDictionary:(NSData *)jsonData{
    NSError *error = nil;
    id jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:&error];
    if (jsonObject != nil && error == nil){
        return jsonObject;
    }else{
        // 解析错误
        return nil;
    }
}



- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    
    if (alertView.tag == 123303) {
        [self dismissViewControllerAnimated:YES completion:nil];
    }else{
        
    }
    
    
    
    
    
}
- (void)addMyOrderTopView {
    
    UIImageView *image1 = [[UIImageView alloc] initWithFrame:CGRectMake(10*NORMWIDTH, (self.secondView.frame.size.height-20)/2, 20*NORMWIDTH, 20*NORMHEIGHT)];
    image1.image = [UIImage imageNamed:@"goback-icon.png"];
    [self.secondView addSubview:image1];
    self.leftBtn.hidden = YES;
    
    UIButton *goBackBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    goBackBtn.frame = CGRectMake(0, 0, self.secondView.frame.size.height, self.secondView.frame.size.height);
    [goBackBtn addTarget:self action:@selector(gobackClick) forControlEvents:UIControlEventTouchUpInside];
    [self.secondView addSubview:goBackBtn];
    self.titleLabel.text = @"修改支付密码";

}

- (void)gobackClick {
    [self dismissViewControllerAnimated:YES completion:nil];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
